<?php
namespace App\DBs;

use App\DBs\BaseDB;
use Illuminate\Support\Facades\DB;

class BugDealDB extends BaseDB
{
    /**
     * BUG处理
     * deal 解决人UID
     * almTime 预估时间
     * detail 处理描述
     * leave 遗留问题
     */

    protected $table = 'bug_deal';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 列表
     */
    public function getIndex()
    {
        $models = DB::table($this->table)->orderBy('id','desc')->get();
        $dataArr = array();
        if (!count($models)) {
            return array('code'=>-1, 'msg'=>'没有数据');
        }
        foreach ($models as $model) {
            $almName = $this->getAdminName($model->deal);
            $almTime = $model->almTime ? date('Y-m-d',$model->almTime) : '未定义';
            $createTime = $model->created_at ? date('Y-m-d',$model->created_at) : '未定义';
            $dataArr[] = [
                'dealId' => $model->id,
                'dealName' => $model->deal,
                'almTime' => $almTime,
                'dealDetail' => $model->detail,
                'leave' => $model->leave,
                'createTime' => $createTime,
            ];
            //解决图片
            $dataArr['imgCount'] = $this->isImg($model->deal,2,$model->id);
        }
        return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
    }

    /**
     * 新增
     */
    public function setAdd($deal,$detail)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->table)->insertGetId([
                'deal' => $deal,
                'detail' => $detail,
                'almTime' => time(),
                'leave' => '',
                'created_at' => time(),
            ]);
            $dataArr = array(
                'proId' => $lastId,
            );
            //提交事务
            DB::commit();
            return array('code'=>0, 'msg'=>'操作成功', 'data'=>$dataArr);
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>-1, 'msg'=>'操作失败：'.$e);
        }
    }
}